import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Events } from 'ionic-angular';
import 'rxjs/add/operator/map';

//聊天信息的属性
export class ChatMessage{
  messageId:string;
  userId:string;
  username:string;
  userImgUrl:string;
  toUserId:string;
  time: number | string;
  message: string;
  status: string;
}
//用户信息的属性
export class UserInfo{
  userId: string;
  userName: string;
  userImgUrl: string;
}

@Injectable()
export class ChatserviceProvider {

  constructor(public http: Http,
              public event: Events) {
    
  }

  /**
   *获取消息列表
   *从 API 获取（json）
   * @returns {Promise<ChatMessage[]>}
   * @memberof ChatserviceProvider
   */
  getMessageList():Promise<ChatMessage[]>{
    const url = '../../assets/mock/msg-list.json';
    return this.http.get(url)
    .toPromise()
    .then(response => response.json().array as ChatMessage[])
    .catch(error=>Promise.reject(error || '错误信息'));
  }

  sendMessage(message:ChatMessage){
    return new Promise(resolve=> setTimeout(()=>{
      resolve(message)
    },Math.random() * 1000))
    .then(()=>{
      this.mockNewMessage(message)
    });
  }

  /**
   *模拟对方回复了一个消息
   *前台合适即时地能接收到消息
   *Event 发布者/订阅者
   * @param {*} message
   * @memberof ChatserviceProvider
   */
  mockNewMessage(message:any){
    const id = Date.now().toString();
    let messageSend : ChatMessage = {
      messageId : id,
      userId: '123321',
      username: '慕女神',
      userImgUrl: 'http://img.mukewang.com/user/57a322f00001e4ae02560256-40-40.jpg',
      toUserId:message.userId,
      time:Date.now(),
      message:'你是不是刚才给我发送了「'+message.message+'」 ? ',
      status:'success'
    }

    //进行 消息的发布
    setTimeout(()=>{
      this.event.publish('chat.received',messageSend,Date.now())
    },Math.random()*1000)


  }

}
